package com.xb.loan.cashier.biz.pay.bankcomm.config;

import com.xb.loan.cashier.biz.pay.unionpay.config.UnionPayConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

public class BankcommConfig {
	
	private static Logger log = LoggerFactory.getLogger(BankcommConfig.class);

	public static final String FILE_NAME = "config/bankcomm_config.properties";
	
	// 从配置文件中直接读出来的信息
	private String pfxPath = "bankcomm.pfxPath";
	private String pfxPassword = "bankcomm.pfxPassword";
	private String cerPath = "bankcomm.cerPath";
	private String bankDN = "bankcomm.bankDN";
	private String httpsType = "bankcomm.httpsType";
	//根据配置文件转换出来的信息
	private byte[] cerByte;
	private byte[] pfxByte;
	
	/** 操作对象. */
	private static BankcommConfig config;
	/** 属性文件对象. */
	private Properties properties;


	/**
	 * 获取config对象.
	 * 
	 * @return
	 */
	public static BankcommConfig getConfig() {
		if (null == config) {
			config = new BankcommConfig();
			config.loadPropertiesFromSrc();
		}
		return config;
	}
	
	public BankcommConfig() {
        super();
    }

	
	/**
	 * 从classpath路径下加载配置参数
	 */
	public void loadPropertiesFromSrc() {
		InputStream in = null;
		try {
			// Properties pro = null;
			log.debug("从classpath: " +UnionPayConfig.class.getClassLoader().getResource("").getPath()+" 获取属性文件"+FILE_NAME);
			in = UnionPayConfig.class.getClassLoader()
					.getResourceAsStream(FILE_NAME);
			if (null != in) {
				BufferedReader bf = new BufferedReader(new InputStreamReader(
						in, "utf-8"));
				properties = new Properties();
				try {
					properties.load(bf);
				} catch (IOException e) {
					throw e;
				}
			} else {
				log.error(FILE_NAME + "属性文件未能在classpath指定的目录下 "+UnionPayConfig.class.getClassLoader().getResource("").getPath()+" 找到!");
				return;
			}
			this.pfxPath = properties.getProperty(this.pfxPath);
			this.pfxPassword = properties.getProperty(this.pfxPassword);
			this.cerPath = properties.getProperty(this.cerPath);
			this.bankDN = properties.getProperty(this.bankDN);
			this.httpsType = properties.getProperty(this.httpsType);
			this.convertByte();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}

	//将证书文件转换成byte
	private void convertByte(){
		try{
			log.debug("convertByte pfxPath="+pfxPath);
			InputStream is1 = new FileInputStream(this.pfxPath);
			ByteArrayOutputStream byteArray1 = new ByteArrayOutputStream();
			int byteData1 = 0;
			while ((byteData1 = is1.read()) != -1) {
				byteArray1.write(byteData1);
			}
			is1.close();
			pfxByte = byteArray1.toByteArray();
			byteArray1.close();
		}catch(Exception e){
			log.error("加载证书文件失败",e);
		}
		try{
			log.debug("convertByte cerPath="+cerPath);
			InputStream is2 = new FileInputStream(this.cerPath);
			ByteArrayOutputStream byteArray2 = new ByteArrayOutputStream();
			int byteData2 = 0;
			while ((byteData2 = is2.read()) != -1) {
				byteArray2.write(byteData2);
			}
			is2.close();
			cerByte = byteArray2.toByteArray();
			byteArray2.close();
		}catch(Exception e){
			log.error("加载证书文件失败",e);
		}
	}
	
	public String getPfxPath() {
		return pfxPath;
	}

	public String getPfxPassword() {
		return pfxPassword;
	}

	public String getCerPath() {
		return cerPath;
	}

	public String getBankDN() {
		return bankDN;
	}

	public String getHttpsType() {
		return httpsType;
	}

	public byte[] getCerByte() {
		return cerByte;
	}

	public byte[] getPfxByte() {
		return pfxByte;
	}
	
	public static void main(String[] args) {
		System.out.println(BankcommConfig.getConfig().getPfxPassword());
	}

}
